package com.jnu.supermarket;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class InputActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);

        Intent intent = getIntent();
        int position = intent.getIntExtra("position",0);

        //统一处理由MainActivity传进来的数据
        EditText editTextName = findViewById(R.id.edit_text_name);
        EditText editTextPrice = findViewById(R.id.edit_text_price);
        String name=intent.getStringExtra("name");//取数据
        Double price = intent.getDoubleExtra("price",0);
        if(null!=name){//name为空，说明是添加数据，没有传name进来
            editTextName.setText(name);
            editTextPrice.setText(String.valueOf(price));
        }

        Button buttonOK = this.findViewById(R.id.button_ok);
        buttonOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("position",position);
                intent.putExtra("name",editTextName.getText().toString());
                intent.putExtra("price",Double.parseDouble(editTextPrice.getText().toString()));
                setResult(ShopItemFragment.RESULT_CODE_ADD_DATA,intent);
                InputActivity.this.finish();

            }
        });
    }
}